package database;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import fingerprint.*;

public class InsertSF {

     Connect nc = new Connect();
     Statement ent = null;
     String sql = null;

    public InsertSF() {
         nc.DConnect();
    }

    public void Insert(ArrayList<ShazamHash> hashes) {
        try {
            ent = nc.nect.createStatement();
            PreparedStatement psql = nc.nect.prepareStatement("insert into song_finger (finger_id,id,offset) values (?,?,?)");
            for (int i = 0; i < hashes.size(); i++) {
                ShazamHash h = hashes.get(i);
                psql.setInt(1,h.finger_id);
                psql.setInt(2,h.id);
                psql.setInt(3,h.offset);
                psql.addBatch();
                if(i%1000 == 0) {
                    psql.executeBatch();
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        if(ent != null){
            try {
                ent.close();
            } catch (SQLException e) {

                e.printStackTrace();
            }
        }
    }
    public void Insertsong(String[] names) {
        long startMillis = System.currentTimeMillis();
        try {
            PreparedStatement statement = nc.nect.prepareStatement("insert into song (id,name) values (?,?)");
            for (int i = 0; i <names.length; i++) {
                statement.setNString(2,names[i]);
                statement.setInt(1,i);
                statement.addBatch();
            }
            statement.executeBatch();
            System.out.println("song表插入完成，用时："+String.valueOf(System.currentTimeMillis()-startMillis)+"ms");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
